<template>
    <div>
        <el-card class="!border-none mb-4" shadow="never">
            <el-form class="mb-[-16px]" :model="queryParams" inline>
                <el-form-item label="商品编号" prop="number">
                    <el-input class="w-[280px]" v-model="queryParams.number" clearable placeholder="请输入商品编号" />
                </el-form-item>
                <el-form-item label="商品全名（管家婆）" prop="goodName">
                    <el-input class="w-[280px]" v-model="queryParams.goodName" clearable placeholder="请输入商品全名（管家婆）" />
                </el-form-item>
                <el-form-item label="美菜商品全名" prop="mcName">
                    <el-input class="w-[280px]" v-model="queryParams.mcName" clearable placeholder="请输入美菜商品全名" />
                </el-form-item>
                <el-form-item label="快驴商品全名" prop="klName">
                    <el-input class="w-[280px]" v-model="queryParams.klName" clearable placeholder="请输入快驴商品全名" />
                </el-form-item>
                <el-form-item label="优大集商品全名" prop="ydjName">
                    <el-input class="w-[280px]" v-model="queryParams.ydjName" clearable placeholder="请输入优大集商品全名" />
                </el-form-item>
                <el-form-item>
                    <el-button type="primary" @click="resetPage">查询</el-button>
                    <el-button @click="resetParams">重置</el-button>
                </el-form-item>
            </el-form>
        </el-card>
        <el-card class="!border-none" v-loading="pager.loading" shadow="never">
            <el-button v-perms="['sku.sku_mapping/add']" type="primary" @click="handleAdd">
                <template #icon>
                    <icon name="el-icon-Plus" />
                </template>
                新增
            </el-button>
            <el-button v-perms="['sku.sku_mapping/delete']" :disabled="!selectData.length"
                @click="handleDelete(selectData)">
                删除
            </el-button>
            <el-button type="danger" @click="handleClearn(selectData)"> 清空数据表 </el-button>
            <el-button type="primary" @click="handleUpload">上传对照表</el-button>
            <el-button type="primary" @click="exportExcel">导出当前对照表</el-button>
            <div class="mt-4">
                <el-table :data="pager.lists" @selection-change="handleSelectionChange">
                    <el-table-column type="selection" width="55" />
                    <el-table-column label="商品编号" prop="number" show-overflow-tooltip />
                    <el-table-column label="商品全名（管家婆）" prop="goodName" show-overflow-tooltip />
                    <el-table-column label="美菜商品全名" prop="mcName" show-overflow-tooltip />
                    <el-table-column label="美菜规格" prop="mcUnit" show-overflow-tooltip />
                    <el-table-column label="快驴商品全名" prop="klName" show-overflow-tooltip />
                    <el-table-column label="快驴规格" prop="klUnit" show-overflow-tooltip />
                    <el-table-column label="优大集商品全名" prop="ydjName" show-overflow-tooltip />
                    <el-table-column label="优大集规格" prop="ydjUnit" show-overflow-tooltip />
                    <el-table-column label="商品单位" prop="unit" show-overflow-tooltip />
                    <el-table-column label="单价" prop="price" show-overflow-tooltip />
                    <el-table-column label="操作" width="120" fixed="right">
                        <template #default="{ row }">
                            <el-button v-perms="['sku.sku_mapping/edit']" type="primary" link @click="handleEdit(row)">
                                编辑
                            </el-button>
                            <el-button v-perms="['sku.sku_mapping/delete']" type="danger" link
                                @click="handleDelete(row.id)">
                                删除
                            </el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </div>
            <div class="flex justify-end mt-4">
                <pagination v-model="pager" @change="getLists" />
            </div>
        </el-card>
        <edit-popup v-if="showEdit" ref="editRef" :dict-data="dictData" @success="getLists" @close="showEdit = false" />
        <UploadPopup v-if="showUpload" ref="uploadRef" @refresh="getLists" @close="showEdit = false" />
    </div>
</template>

<script lang="ts" setup name="skuMappingLists">
import { usePaging } from '@/hooks/usePaging'
import { useDictData } from '@/hooks/useDictOptions'
import {
    apiSkuMappingLists,
    apiSkuMappingDelete,
    apiSkuMappingUploadExcel,
    clearMapping
} from '@/api/sku_mapping'
import feedback from '@/utils/feedback'
import EditPopup from './edit.vue'
import UploadPopup from './sku-upload.vue'
const editRef = shallowRef<InstanceType<typeof EditPopup>>()
const uploadRef = shallowRef<InstanceType<typeof EditPopup>>()
// 是否显示编辑框
const showEdit = ref(false)
// 是否显示上传框
const showUpload = ref(false)

// 查询条件
const queryParams = reactive({
    number: '',
    goodName: '',
    mcName: '',
    klName: '',
    ydjName: ''
})

// 选中数据
const selectData = ref<any[]>([])

// 下载表格
const exportExcel = () => {
    const data = {
        export: 2,
        number: '',
        goodName: '',
        mcName: '',
        klName: '',
        ydjName: '',
        page_type: 0
    }
    apiSkuMappingLists(data).then((res) => {
        console.log(res)
    })
}

// 表格选择后回调事件
const handleSelectionChange = (val: any[]) => {
    selectData.value = val.map(({ id }) => id)
}

// 获取字典数据
const { dictData } = useDictData('')

// 分页相关
const { pager, getLists, resetParams, resetPage } = usePaging({
    fetchFun: apiSkuMappingLists,
    params: queryParams
})

// 添加
const handleAdd = async () => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('add')
}

// 编辑
const handleEdit = async (data: any) => {
    showEdit.value = true
    await nextTick()
    editRef.value?.open('edit')
    editRef.value?.setFormData(data)
}

// 上传
const handleUpload = async (data: any) => {
    showUpload.value = true
    await nextTick()
    uploadRef.value?.open('edit')
}

// 删除
const handleDelete = async (id: number | any[]) => {
    await feedback.confirm('确定要删除？')
    await apiSkuMappingDelete({ id })
    getLists()
}
// 清空
const handleClearn = async () => {
    if (pager.lists.length <= 0) {
        feedback.alertError('列表为空')
        return
    }
    await feedback.confirm('确定要清空该公司的对照表？')
    await clearMapping()
    getLists()
}

getLists()
</script>
